// JavaScript Document
/**
 * Ajax
 * 
 */

function getXmlHttp() {
	var xmlHttp = null;
	/* @cc_on @ */
	/*
	 * @if (@_jscript_version >= 5) try { xmlHttp = new
	 * ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new
	 * ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } }
	 * @end @
	 */
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}
function postXMLHttpRequest(url, form, objectid) {
	dojo.io.iframe
			.send({
				url : url,
				method : "post",
				contentType : "multipart/form-data",
				handleAs : "text/html",
				form : dojo.byId(form),
				handle : function(data, ioArgs) {
					if (data.substring(0, 13) == "downloadFile=") {
						var responseBodyOffset = data.indexOf("\n", 13);
						var fileName = unescape(data.substring(13,
								responseBodyOffset));
						data = data.substring(responseBodyOffset);
						downloadFile(fileName);
					}

					setInnerHTML(objectid, data);
					// dojo.byId(objectid).innerHTML = data;
					clearDojoWidget(data);
					createPageTable(data);
					creatSortTable(data);
					dojo.parser.parse(dojo.byId(objectid));
				}
			});

	document.getElementById(objectid).innerHTML = "<img src='images/roller.gif' />processing, please wait...";
}

function sendXMLHttpRequest(url, content, objectid) {
	document.getElementById(objectid).innerHTML = "<img src='images/roller.gif' />processing, please wait...";

	date = new Date();
	if (url.indexOf("?") > -1) {
		myurl = url + "&timestamp=" + date.getTime();
	} else {
		myurl = url + "?timestamp=" + date.getTime();
	}

	var xmlHttp = getXmlHttp();
	xmlHttp.open("POST", myurl, true);
	xmlHttp.setRequestHeader("Content-Length", content.length);
	xmlHttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var response = xmlHttp.responseText;
				if (response.substring(0, 9) == "login.jsp") {
					// alert(1);
					response = response.substring(9);
					setInnerHTML("container", response);
					// document.getElementById("container").innerHTML =
					// response;
				} else if (response.substring(0, 13) == "downloadFile=") {
					var responseBodyOffset = response.indexOf("\n", 13);
					var fileName = response.substring(13, responseBodyOffset);
					response = response.substring(responseBodyOffset);
					setInnerHTML(objectid, response);
					// document.getElementById(objectid).innerHTML = response;
					downloadFile(fileName);
				} else {
					setInnerHTML(objectid, response);
					// document.getElementById(objectid).innerHTML = response;
				}

				// document.getElementById(objectid).innerHTML = response;
				clearDojoWidget(response);
				createPageTable(response);
				creatSortTable(response);
				dojo.parser.parse(dojo.byId(objectid));
			} else if (xmlHttp.status == 404) {
				document.getElementById(objectid).innerHTML = "Requested URL is not found.";
			} else if (xmlHttp.status == 403) {
				document.getElementById(objectid).innerHTML = "Access denied.";
			} else {
				if (xmlHttp.status != 302) {
					var response = "response error!<br />"
							+ "http status: <strong>" + xmlHttp.status
							+ "</strong><br />" + " please try again later!";
					setInnerHTML(objectid, response);
					// document.getElementById(objectid).innerHTML = response;
				}
			}
		}
	};
	xmlHttp.send(content);
}

function creatSortTable(response) {
	if (response.match(/id="sort_table"/gi)) {
		// ts_resortTableInit2("sort_table");
		sortTable = new SortTable("sort_table", "sort_body");
	}
}

function createPageTable(response) {
	if (response.match(/id="page_table"/gi)) {
		page = new Page(20, "page_table", "page_body", "page_index");
	}
}

function parseDojoWidget(objectid, response) {
	dojo.parser.parse(dojo.byId(objectid));
	var m = response.match(/<div.*\sid=\".*\"\s/gi);
	for ( var n = 0; m != null && n < m.length; n++) {
		var id = m[n].substring(m[n].indexOf(" id=\""));
		id = id.substring(5, id.indexOf("\""));
		dojo.parse.parse(dojo.byId(id));
	}
}

function clearDojoWidget(response) {
	// regex = new RegExp("id=\".*\".*\sdojoType=\"dijit.form.*\"", "gi");
	var m = response.match(/id=".*".*\sdojoType="dijit.form.*"/gi);
	for ( var n = 0; m != null && n < m.length; n++) {
		var id = m[n].substring(4, m[n].indexOf(" ") - 1);
		// alert(id);
		if (dijit.byId(id))
			dijit.byId(id).destroy();
	}
}

var scriptHistory = new Array();

String.prototype.trim = function() {
	// 用正则表达式将前后空格
	// 用空字符串替代。
	return this.replace(/(^\s*)|(\s*$)/g, "");
};

function compressHtml(html) {
	var s = "";
	var idx = -1;
	while ((idx = html.indexOf("\n")) != -1) {
		s += html.substring(0, idx).trim();
		html = html.substring(idx + 1);
	}
	if (html.length > 0)
		s += html.trim();
	return s;
}

function setInnerHTML(objectid, htmlcode) {
	var temp = htmlcode.replace(/\n|\r/g, "");
	var regex = /<script.+?<\/script>/gi;
	var arr = temp.match(regex);
	// alert(arr);
	if (arr != null) {
		for ( var i = 0; i < arr.length; i++) {
			var be = false;
			for ( var n = 0; n < scriptHistory.length; n++) {
				if (scriptHistory[n] == arr[i]) {
					be = true;
					break;
				}
			}
			if (be == true)
				continue;

			scriptHistory.push(arr[i]);
			var script = document.createElement("script");
			script.setAttribute("type", "text/javascript");
			script.text = arr[i].replace(/<script.+?>/gi, "").replace(
					/<\/script>/gi, "");
			// alert(script.text);
			document.getElementsByTagName("head")[0].appendChild(script);
		}
	}
	document.getElementById(objectid).innerHTML = temp.replace(regex, "");
}

function downloadFile(file) {
	var elemIF = document.createElement("iframe");
	elemIF.src = "download.do?file=" + file;
	elemIF.style.display = "none";
	document.body.appendChild(elemIF);
}
